﻿using Configs;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace Configs
{
    public class InputReaderMan
    {
        public bool UseJoy { get; } = false;
        private Transform transform;
        private readonly float aimDistance = 500f;
        private Vector3 aimV3;
        private Vector3 srceenCenten;
        private int frame = -1;

        /// <summary>
        /// 使用键鼠控制
        /// </summary>
        public InputReaderMan()
        {
            InputConfigDic.Add(0, new InputReaderConfig());
        }
        /// <summary>
        /// 使用手柄控制
        /// </summary>
        /// <param name="transform">受控角色的Transform</param>
        public InputReaderMan(Transform transform)
        {
            this.transform = transform;
            UseJoy = true;
            srceenCenten = new Vector3(Screen.width / 2F, Screen.height / 2f, 0);
            InputConfigDic.Add(0, new InputReaderConfig());
        }

        public Dictionary<int, InputReaderConfig> InputConfigDic = new Dictionary<int, InputReaderConfig>();

        public bool Run { get { return CheckInput(Action.Run); } }



        public bool InterActive { get { return CheckInputDown(Action.InterActive); } }

        /// <summary>
        /// 滚
        /// </summary>
        public bool Roll { get { return CheckInput(Action.Roll); } }

        #region 攻击
        /// <summary>
        /// 某攻击动作开始
        /// </summary>
        /// <param name="action">攻击动作(AttackXXXStart)</param>
        /// <returns></returns>
        public bool AttackStartIns(Action action)
        {
            return CheckInputDown(action);
        }

        /// <summary>
        /// 某攻击动作结束
        /// </summary>
        /// <param name="action">(AttackXXXEnd)</param>
        /// <returns></returns>
        public bool AttackEndIns(Action action)
        {
            return CheckInputUp(action);
        }
        #endregion

        public float HorizontalAxis
        {
            get
            {
                float h = Input.GetAxis(InputConfigDic[0].Raws_MoveH);
                return h;
            }
        }

        public float VerticalAxis
        {
            get
            {
                float v = Input.GetAxis(InputConfigDic[0].Raws_MoveV);
                return v;
            }
        }


        private bool CheckInput(Action index)
        {
            foreach (var item in InputConfigDic.Values)
            {
                if (Input.GetKey(item.keyCodes[(int)index]))
                {
                    return true;
                }
            }
            return false;
        }

        private bool CheckInputDown(Action index)
        {
            foreach (var item in InputConfigDic.Values)
            {
                if (Input.GetKeyDown(item.keyCodes[(int)index]))
                {
                    return true;
                }
            }
            return false;
        }

        private bool CheckInputUp(Action index)
        {
            foreach (var item in InputConfigDic.Values)
            {
                if (Input.GetKeyUp(item.keyCodes[(int)index]))
                {
                    return true;
                }
            }
            return false;
        }


    }

    public class InputReaderConfig
    {
        public KeyCode[] keyCodes { get; private set; } = new KeyCode[] {
            KeyCode.W,
            KeyCode.S,
            KeyCode.A,
            KeyCode.D,
            KeyCode.LeftShift,
            KeyCode.E,
            KeyCode.Space,
            KeyCode.Mouse0,
            KeyCode.Mouse1,
            KeyCode.Alpha1,
            KeyCode.Alpha2,
            KeyCode.Alpha3,
            KeyCode.Alpha4,
        };
        public string Raws_MoveH = "Horizontal", Raws_MoveV = "Vertical";
        public string Raws_AimH = "J1_RH", Raws_AimV = "J1_RV";
    }

    public enum Action
    {
        Up,
        Down,
        Left,
        Right,
        Run,
        InterActive,
        Roll,
        AttackNormal,
        AttackSpecial,
        AttackSkill1,
        AttackSkill2,
        AttackSkill3,
        AttackSkill4,
    }
}
